﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CapstoneProjectAEReader.DataAccess.DBAccess;
using CapstoneProjectAEReader.Entities.Models;
using CapstoneProjectAEReader.Common.Utility;
using CapstoneProjectAEReader.Business.Others;
using CapstoneProjectAEReader.GUIs.TranslationGUI;

namespace CapstoneProjectAEReader.GUIs.SettingGUI
{
    /// <summary>
    /// Interaction logic for Setting.xaml
    /// </summary>
    public partial class Setting : UserControl
    {
        #region Attributes

        private DAO _dao;
        private SyncManager _syncManager;
        private FileHandler _fileHandler;
        private MainWindow _parent;
        private const string FontHeader = "fontHeader";
        private const string FontType = "fontType";
        private const string FontSizeLb = "fontSizeLb";
        private const string DisplayHeader = "displayHeader";
        private const string ReadingMode = "readingMode";
        private const string DayMode = "dayMode";
        private const string NightMode = "nightMode";
        private const string LanguageLb = "language";
        private const string Services = "services";
        private const string BtUpload = "btUpload";
        private const string BtDownLoad = "btDownload";
        private const string BtSave = "saveButton";
        private const string BtCancel = "cancelButton";
        private const string OnlineStorage = "onlineStorage";
        private const string EbookTabName = "ebookTab";
        private const string NoteTabName = "noteTab";
        private const string SettingName = "settingTab";
        private const string SyncingInProcess = "syncingInProcess";
        private const string ComfirmDownloadMes = "comfirmDownloadMes";
        private const string SaveSetting = "saveSetting";
        private Thread _thread;

        #endregion

        public Setting(DTO dto, MainWindow parent)
        {
            _parent = parent;
            _dao = new DAO(dto);
            _fileHandler = new FileHandler();
            _syncManager = new SyncManager(0);
            InitializeComponent();
            ReloadFormLanguage();
        }

        public Grid GetSettingTab()
        {
            return mainGrid;
        }

        public void ReloadFormLanguage()
        {
            gbFont.Header = ResourceReader.GetResourceContent(FontHeader);
            gbDisplay.Header = ResourceReader.GetResourceContent(DisplayHeader);
            gbOnlineStorage.Header = ResourceReader.GetResourceContent(OnlineStorage);
            lbFontSize.Content = ResourceReader.GetResourceContent(FontSizeLb);
            lbFontType.Content = ResourceReader.GetResourceContent(FontType);
            lbLanguage.Content = ResourceReader.GetResourceContent(LanguageLb);
            lbReadingMode.Content = ResourceReader.GetResourceContent(ReadingMode);
            lbServices.Content = ResourceReader.GetResourceContent(Services);
            //btCancel.Content = ResourceReader.GetResourceContent(BtCancel);
            btDownload.Content = ResourceReader.GetResourceContent(BtDownLoad);
            btSave.Content = ResourceReader.GetResourceContent(BtSave);
            btUpload.Content = ResourceReader.GetResourceContent(BtUpload);
            rbDay.Content = ResourceReader.GetResourceContent(DayMode);
            rbNight.Content = ResourceReader.GetResourceContent(NightMode);
            cbFontSize.Items.Clear();
            ComboBoxItem temp = new ComboBoxItem();
            for (int i = 8; i < 32; i += 2)
            {
                cbFontSize.Items.Add(i);
            }
            cbFontSize.SelectedValue = _dao.Dto.SettingDetail.FontSize;
            cbFontType.SelectedItem = _dao.Dto.SettingDetail.SelectedFontType;
            if (_dao.Dto.SettingDetail.ApplicationLanguage.Equals(Constants.SettingSupporter.Vietnamese))
            {
                cbLanguage.SelectedIndex = 0;
            }
            else
            {
                cbLanguage.SelectedIndex = 1;
            }
            if (Constants.SettingSupporter.DayReadingMode == _dao.Dto.SettingDetail.ReadingMode)
            {
                rbDay.IsChecked = true;
                rbNight.IsChecked = false;
            }
            else
            {
                rbNight.IsChecked = true;
                rbDay.IsChecked = false;
            }
        }

        private void HandleBtUpload(object sender, RoutedEventArgs e)
        {
            if (_thread == null || !_thread.IsAlive)
            {
                var newthread = new Thread(() =>
                                               {
                                                   _syncManager = new SyncManager(0);
                                                   _syncManager.UploadHandler(_dao.Dto);
                                               });
                newthread.SetApartmentState(ApartmentState.STA);
                newthread.IsBackground = true;
                newthread.Start();
                _thread = newthread;
            }
            else
            {
                Utilities.ShowWarningMesbox(ResourceReader.GetResourceContent(SyncingInProcess));
            }
        }


        //public void closeStatusBar()
        //{
        //    _parent.SetStatus("");
        //}

        private void HandleBtDownload(object sender, RoutedEventArgs e)
        {
            if (_thread == null || !_thread.IsAlive)
            {
                //    Thread newthread = new Thread(() =>
                //    {
                if (MessageBoxResult.OK ==
                    Utilities.ShowConfirmationMesBox(
                        ResourceReader.GetResourceContent(ComfirmDownloadMes)))
                {
                    _syncManager = new SyncManager(0);
                    _syncManager.DownloadHandler(_parent, _dao, _fileHandler);
                    //});
                    //newthread.SetApartmentState(ApartmentState.STA);
                    //newthread.IsBackground = true;
                    //newthread.Name = "SyncThread";
                    //newthread.Start();
                    //_thread = newthread;
                    _parent.CloseAllTab();
                }
            }
            else
            {
                Utilities.ShowWarningMesbox(ResourceReader.GetResourceContent(SyncingInProcess));
            }
        }

        private void HandleSaveBt(object sender, RoutedEventArgs e)
        {
            _dao.Dto.SettingDetail.FontSize = int.Parse(cbFontSize.SelectionBoxItem.ToString());
            cbFontSize.SelectedItem = cbFontSize.SelectionBoxItem;
            _dao.Dto.SettingDetail.SelectedFontType = cbFontType.SelectionBoxItem.ToString();
            cbFontType.SelectedItem = cbFontType.SelectionBoxItem;
            if (rbDay.IsChecked == true)
            {
                _dao.Dto.SettingDetail.ReadingMode = Constants.SettingSupporter.DayReadingMode;
            }
            else
            {
                _dao.Dto.SettingDetail.ReadingMode = Constants.SettingSupporter.NightReadingMode;
            }
            _dao.Dto.SettingDetail.ApplicationLanguage =
                Constants.SettingSupporter.SupportedLanguages[cbLanguage.SelectionBoxItem.ToString()];
            string oldEbookTabName = ResourceReader.GetResourceContent(EbookTabName);
            string oldSettingTabName = ResourceReader.GetResourceContent(SettingName);
            string oldNoteTabName = ResourceReader.GetResourceContent(NoteTabName);
            ResourceReader.Initialize(_dao.Dto.SettingDetail.ApplicationLanguage);
            _parent.ReloadEbookTabLanguage(oldEbookTabName);
            _parent.ReloadNoteTabLanguage(oldNoteTabName);
            _parent.ReloadSettingTabLanguage(oldSettingTabName);
            _parent.ReloadEbooksTabLanguage();
            ReloadFormLanguage();
            _parent.ReloadFormLanguage();
            DAO.CommitData(_dao.Dto);
            Utilities.ShowSuccessfulMesbox(ResourceReader.GetResourceContent(SaveSetting));
        }
    }
}